<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css">
    <script src="./js/vue.js"></script>
    <style>

    </style>
</head>

<body>
    <div id="app" class="container">
        <div id="app" class="container">
            <h3 class="text-primary text-center">购物车</h3>
            <table class="table table-light table-bordered">
                <thead>
                    <tr>
                        <th><input type="checkbox" @change="allCheck" v-model="isAllCheck">全选</th>
                        <th>商品名称</th>
                        <th>商品单价</th>
                        <th>商品数量</th>
                        <th>商品总价</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in arr" ::key="item.id">
                        <td><input type="checkbox" v-model="item.isCheck" @change="changeOne"></td>
                        <th>{{item.name}}</th>
                        <th>{{item.price|filterPrice}}</th>
                        <th><button @click="subNum(index)">-</button> {{''+item.num+''}} <button
                                @click="addNum(index)">+</button>
                        </th>
                        <th>{{(item.price*item.num)|filterPrice}}</th>
                        <th><button type="button" class="btn btn-danger" @click="arr.splice(index,1)">删除</button></th>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td>总价</td>
                        <td colspan="5">{{sumPrice|filterPrice}}</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    <script>
        let mock = [{
            id: 1,
            name: "手机",
            price: 8.99,
            num: 1,

        },
        {
            id: 2,
            name: "电脑",
            price: 200,
            num: 2,
        },
        {
            id: 3,
            name: "ipad",
            price: 99,
            num: 1,
        }
        ]

        Vue.filter('filterPrice', (C) => C.toFixed(2))

        let vm = new Vue({
            el: '#app',
            data() {
                return {
                    arr: [],
                    isAllCheck: false,
                }
            },
            mounted() {
                mock.forEach(item => item.isCheck = false)
                this.arr = mock
            },
            methods: {
                subNum(index) {
                    this.arr[index].num--
                    this.arr[index].num = Math.max(0, this.arr[index].num)
                },
                addNum(index) {
                    this.arr[index].num++
                    this.arr[index].num = Math.min(5, this.arr[index].num)
                },
                allCheck() {
                    this.arr.forEach(item => item.isCheck = this.isAllCheck)
                },
                changeOne() {
                    this.isAllCheck = this.arr.every(item => item.isCheck)
                }
            },
            computed: {
                sumPrice() {
                    let sum = 0
                    this.arr.filter(item => item.isCheck == true).forEach(item => sum += item.price * item.num)
                    return sum
                }
            }
        })

    </script>
</body>

</html>